package com.popchan.manager
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.system.System;
	import flash.utils.setTimeout;

	/**
	 *拖动管理
	 *Feedback zingblue@163.com,zingblue@gmail.com
	 *Copyright 2012,chenbo,All rights reserved
	 *
	 */
	public class DragManager
	{
		private static var dragCopy:Sprite;
		private static var dragTarget:DisplayObject;
		private static var dragData:Object;
		private static var dragTargetType:Class;
		private static var isDragging:Boolean=false;
		private static var upFunc:Function;
		private static var moveFunc:Function;
		/**
		 * 开始拖动 
		 * @param target
		 * @param data
		 * @param dragImage
		 * 
		 */
		public static function startDrag(target:DisplayObject,dragImage:DisplayObject,moveF:Function=null,upF:Function=null):void
		{
			if(isDragging)return;
			var stageRef:Stage=target.stage;
			dragTarget=target;
			moveFunc=moveF;
			upFunc=upF;
			if(dragCopy == null){
				dragCopy = new Sprite();
				dragCopy.mouseEnabled = false;
			}else{
				if(dragCopy.parent != null){
					dragCopy.parent.removeChild(dragCopy);
				}
			}
			if(dragCopy.numChildren > 0)
			{
				dragCopy.removeChildAt(0);
			}
			
			
			dragCopy.startDrag(false);
			stageRef.addChild(dragCopy);
			
			if(dragImage==null)
			{
				dragImage=new Shape();
				dragImage.name="dragImg"
				Shape(dragImage).graphics.lineStyle(1);
				Shape(dragImage).graphics.beginFill(0x888888,.4);
				Shape(dragImage).graphics.drawRect(0,0,50,50);
				Shape(dragImage).graphics.endFill();
			}
			
			dragCopy.addChild(dragImage);
			
			var dp:Point = stageRef.globalToLocal(new Point(target.x,target.y));
			dragCopy.x=dp.x;
			dragCopy.y=dp.y;
			
			stageRef.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove, false, 0, true);
			stageRef.addEventListener(MouseEvent.MOUSE_DOWN, onStateMouseDown, false, 0, true);
			stageRef.addEventListener(MouseEvent.MOUSE_UP, onStateMouseUp, false, 0, true);
			
			isDragging=true;
		}
		/**
		 *	获取目的地的目标 
		 * @param stage
		 * @param p
		 * @return 
		 * 
		 */
		private static function getDropTarget(stage:Stage,p:Point,targetType:Class):DisplayObject
		{
			if(stage==null)return null;
			
				
			var list:Array=stage.getObjectsUnderPoint(p);
			for(var i:int=list.length-1;i>=0;i--)
			{
				var obj:DisplayObject=list[i];
				while(!(obj is targetType)&&obj.parent!=stage)
				{
					obj=obj.parent;
					
				}
				return obj;
				/*if(obj is targetType)
					return obj;
				else if(obj.parent is targetType)
				{
					return obj.parent;
				}*/
			}
		
			return null;
		}
		protected static function onStateMouseUp(event:MouseEvent):void
		{
			dragCopy.stopDrag();
			var stageRef:Stage=dragTarget.stage;
			stageRef.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
			stageRef.removeEventListener(MouseEvent.MOUSE_DOWN, onStateMouseDown);
			stageRef.removeEventListener(MouseEvent.MOUSE_UP, onStateMouseUp);
			
		/*	var e:DragEvent=new DragEvent(DragEvent.DRAG_DROP,dragData,dragTarget);
		
			
			var aim:DisplayObject=getDropTarget(stageRef,new Point(stageRef.mouseX,stageRef.mouseY),dragTargetType);
			if(aim!=null)
			{
				trace(aim.name);
				aim.dispatchEvent(e);
				dragTarget.dispatchEvent(e);
			}*/
			
			//dragTarget.x=dragCopy.x;
			//dragTarget.y=dragCopy.y;
			if(dragCopy.parent != null){
				dragCopy.parent.removeChild(dragCopy);
				dragCopy=null;
			}
			isDragging = false;
			if(upFunc!=null)
				upFunc();
			System.gc();
		}
		
		protected static function onStateMouseDown(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			
		}
		
		protected static function onStageMouseMove(event:MouseEvent):void
		{
			if(moveFunc!=null)
				moveFunc();
		}
	}
}